package de.schlinke_marius.fragments;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;
import de.schlinke_marius.activities.ExamplesDetail;
import de.schlinke_marius.adapters.ExampleAdapter;
import de.schlinke_marius.application.MSComputerApplication;
import de.schlinke_marius.data.AppContainer;
import de.schlinke_marius.data.Example;

public class ExamplesFragment extends ListFragment{

	private Activity mActivity;
	private List<Example> examples = null;
	private AppContainer globalData = null;
	private ExampleAdapter exampleAdapter;
	private int mCurCheckPosition = 0;
    boolean mDualPane= false;
	
	@Override
	public void onAttach(Activity activity) {
	
		this.mActivity = activity;	
		MSComputerApplication application = (MSComputerApplication) activity.getApplication();		
		this.globalData = application.getGlobalData();		
		this.examples = this.globalData.getExamples();	
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {	
		exampleAdapter = new ExampleAdapter(mActivity, examples);		
		super.onActivityCreated(savedInstanceState);

		setListAdapter(exampleAdapter);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if(outState.isEmpty()) {
			outState.putBoolean("bug:fix", true);
			outState.putInt("curChoice", mCurCheckPosition);
		}
	}
	@Override
	public void onListItemClick(ListView listview, View view, int position, long id) {
		showDetails(position);
	}
	 void showDetails(int index) {
	        mCurCheckPosition = index;
	        Example example = examples.get(index);
	        int id = example.getId();

	        if (mDualPane) {
	           
	        	//show DetailFragment (Tablet)

	        } else {
	            // Otherwise we need to launch a new activity to display
	            // the dialog fragment with selected text.
	            Intent intent = new Intent();
	            intent.setClass(getActivity(), ExamplesDetail.class);
	            intent.putExtra("id", id);
	            intent.putExtra("index", mCurCheckPosition);
	            startActivity(intent);
	        }
	    }

}
